package GUI.Cocina;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Slider;

import sale.entities.OrderVO;
import sale.entities.SaleVO;
import sale.exceptions.NoSalesException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;
import service.sale.interfaces.SaleProxyMgt;

import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class OrdenesAbiertas extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	OrderVO [] ordenes;
	SaleVO[] sales;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public OrdenesAbiertas(Shell parent, int style) {
		super(parent, style);
		setText("Ordenes Abiertas");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws ProxyException 
	 * @throws RemoteException 
	 * @throws NoSalesException 
	 */
	public Object open() throws RemoteException, ProxyException, NoSalesException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws ProxyException 
	 * @throws RemoteException 
	 * @throws NoSalesException 
	 */
	private void createContents() throws RemoteException, ProxyException, NoSalesException {
		SaleProxyMgt uso = ServiceFacade.getInstance().getSaleProxyMgt();
	/*	SaleVO[] o = uso.getOpenDeliverySales();
		SaleVO [] oo = new SaleVO[o.length + uso.getOpenTableSales().length + uso.getOpenTakeoOutSales().length];
		int j=0;
		for(int i=0;i<o.length;i++){
			oo[i]=o[i];
			j=j+1;
		}
		for(int k=0; k<uso.getOpenTableSales().length;k++){
			oo[j] = uso.getOpenTableSales()[k];
			j=j+1;
		}
		
		for(int p=0;p<uso.getOpenTakeoOutSales().length;p++){
			oo[j] = uso.getOpenTakeoOutSales()[p];
		}
		*/
		/*sales=oo;
		OrderVO [] ords = new;
		for(int m=0;m<sales.length;m++){
			for(int k=0; k<sales[m].getOrders().size();k++){
				
			}
		}
		
		*/
		
		
		shell = new Shell(getParent(), getStyle());
		shell.setSize(720, 337);
		shell.setText(getText());
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 24, 663, 212);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnOrden = new TableColumn(table, SWT.NONE);
		tblclmnOrden.setWidth(64);
		tblclmnOrden.setText("Orden");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("New Column");
		
		TableColumn tblclmnArticulo = new TableColumn(table, SWT.NONE);
		tblclmnArticulo.setWidth(159);
		tblclmnArticulo.setText("Articulo");
		
		TableColumn tblclmnEspecificacion = new TableColumn(table, SWT.NONE);
		tblclmnEspecificacion.setWidth(205);
		tblclmnEspecificacion.setText("Especificacion");
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(155);
		tblclmnNewColumn.setText("Estado");
		
		Button btnMarcarComoListo = new Button(shell, SWT.NONE);
		btnMarcarComoListo.addSelectionListener(new SelectionAdapter() {
			
			public void widgetSelected(SelectionEvent e) {
				if(table.getSelection().length==0){
					
				}
				else{
				TableItem t = table.getSelection()[0];
				
				t.setText(4, "pronto");
				boolean esmesa = false;
				
				if(t.getText(1).equalsIgnoreCase("Mostrador")){
					
					for (int i = 0; i<table.getItemCount();i++){
						if(table.getItem(i).equals(t)){
							table.remove(i);
							
						
					}
					}
					esmesa=true;
					
				}
				else{
					if(t.getText(1).equalsIgnoreCase("Delivery")){
						for (int i = 0; i<table.getItemCount();i++){
							if(table.getItem(i).equals(t)){
								table.remove(i);
								
							
						}
						}
						esmesa=true;
						
					}
					
				}
				
				
				if(esmesa==false){
				
					int m = Integer.parseInt((t.getText(0)));
					String men = t.getText(1);
				if (m>0){
					TableItem[] items = table.getItems();
					ArrayList<TableItem> mesa = new ArrayList<TableItem>();
					for (int i = 0; i<items.length;i++){
						if (items[i].getText(0).equals(t.getText(0))){
							mesa.add(items[i]);
						//	System.out.println("agregue");
						}
						}
					boolean todos =true;
				for (int k=0;k<mesa.size();k++){
					if (mesa.get(k).getText(4).equalsIgnoreCase("esperando coccion")){
						//System.out.println(mesa.get(k).getText(1));
						todos=false;
							
				}
				
				}
				if(todos == true){
					for (int i = 0; i<table.getItemCount(); i++){
						for (int j = 0; j<mesa.size();j++){
							if (mesa.get(j).equals(table.getItem(i))){
								table.remove(i);
							}
						}
					}
					
					OrdenLista o = new OrdenLista (shell, shell.getStyle(),men);
					o.open();
					}
				
				}
					
				}
				
				
					
					
				
			}}
		});
		
		
		btnMarcarComoListo.setBounds(10, 277, 153, 28);
		btnMarcarComoListo.setText("Marcar como listo");
		
		ordenes = uso.getOpenOrders();
		//System.out.println("Numero de ordenes" + ordenes.length + "Aaaaa");
		
		for (int i = 0; i< ordenes.length; i++){
			for (int jj = 0; jj< ordenes[i].getEspecif().size();jj++){
				
				TableItem tableItem = new TableItem(table, SWT.NONE);
				tableItem.setText(0, String.valueOf(ordenes[i].getPrimaryKey()));
				if(ordenes[i].getArts().size()!=0){
				tableItem.setText(2, ordenes[i].getArts().get(jj).getName());
				}
				System.out.println("listo");
				
				tableItem.setText(3,ordenes[i].getEspecif().get(jj));
				
				
				int m = ordenes[i].getTableKey();
				if (m==-1){
					tableItem.setText(1, "Mostrador");
					
				}
				if(m==-2){
					tableItem.setText(1,"Delivery");
				}
				if (m>=0){
					tableItem.setText(1,"Mesa" + " " + String.valueOf(m));
				}
				
				tableItem.setText(4, "Esperando coccion");
			}
		}
		
	}
}
